﻿using PMS.Client.Entities;
using PMS.Client.IBLL;
using PMS.Client.IDAL;
using PMS.Client.Utils;

namespace PMS.Client.BLL
{
    public class UserService : ServiceBase, IUserService
    {
        IUserAccess _userAccess;
        public UserService(IUserAccess userAccess)
        {
            _userAccess = userAccess;
        }

        public EmployeeEntity Login(string username, string password)
        {
            string json = _userAccess.Login(username, password);
            //Result<EmployeeEntity> result = JsonUtil.Deserializer<Result<EmployeeEntity>>(json);
            //if (result.state != 200)
            //    throw new Exception(result.exceptionMessage);


            ////将Entity -》  Model     如果这样处理的话，需要将所有Model独立到一个程序集中
            //return result.data;
            return this.GetResult<EmployeeEntity>(json);
        }

        public bool UpdatePassword(int id, string opd, string npd)
        {
            string json = _userAccess.UpdatePassword(id, opd, npd);
            //Result<bool> result = JsonUtil.Deserializer<Result<bool>>(json);
            //if (result.state != 200)
            //    throw new Exception(result.exceptionMessage);


            //return result.data;
            return this.GetResult<bool>(json);
        }


        public EmployeeEntity[] GetUsers(string key)
        {
            string json = _userAccess.GetUsers(key);
            //Result<EmployeeEntity[]> result = JsonUtil.Deserializer<Result<EmployeeEntity[]>>(json);
            //if (result.state != 200)
            //    throw new Exception(result.exceptionMessage);

            //return result.data;
            return this.GetResult<EmployeeEntity[]>(json);
        }

        public int UpdateUser(EmployeeEntity entity)
        {
            string json = JsonUtil.Serializer(entity);
            json = _userAccess.UpdateUser(json);

            return this.GetResult<int>(json);
        }

        public int DeleteUser(int id)
        {
            string result = _userAccess.DeleteUser(id);
            return this.GetResult<int>(result);
        }

        public bool LockUser(int id, int status)
        {
            string json = _userAccess.LockUser(id, status);
            return this.GetResult<bool>(json);
        }

        public bool CheckUserName(string username, int id)
        {
            string json = _userAccess.CheckUserName(username, id);
            return this.GetResult<bool>(json);
        }

        public int SaveUserRoles(RoleUser[] roleUsers)
        {
            string json = JsonUtil.Serializer(roleUsers);
            json = _userAccess.SaveUserRoles(json);
            return this.GetResult<int>(json);
        }

        public int ResetPassword(int id)
        {
            string json = _userAccess.ResetPassword(id);
            return this.GetResult<int>(json);
        }

        public EmployeeEntity[] GetUsersByIds(int[] ids)
        {
            string json = JsonUtil.Serializer(ids);
            json = _userAccess.GetUsersByIds(json);
            return this.GetResult<EmployeeEntity[]>(json);
        }
    }
}
